//
//  InfoController.m
//  ArchivingObjs
//
//  Created by Wang Shao-chuan on 2010/12/28.
//  Copyright 2010 National Taiwan University. All rights reserved.
//

#import "InfoController.h"


@implementation InfoController
-(void) awakeFromNib
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	NSString *greetingText = [[mGreetingText stringValue] copy];
	
	NSString *greetingWithName = [[NSString alloc] initWithFormat:@"%@ %@", greetingText, NSUserName()];
	[mGreetingText setStringValue: greetingWithName];
	
	[greetingText release];
	[greetingWithName release];
	
	NSFileManager *fileManager = [[NSFileManager alloc] init];
	if ([fileManager fileExistsAtPath: [self defaultFilePath]]) {
		NSData *dataArea = [NSData dataWithContentsOfFile: [self defaultFilePath]];
		if (!dataArea) {
			NSLog(@"Cannot unarchive the info.pkl");
			goto exit;
		}
		NSLog(@"use archived.");
		mInfo = [[NSKeyedUnarchiver unarchiveObjectWithData: dataArea] retain];
	} else {
		NSLog(@"use new personInfo");
		mInfo = [PersonInfo new];
	}
	[self updateUI];
	[self initSlider];
	bSaved = YES;
	[NSApp setDelegate: self];
exit:
	[fileManager release];
	[pool drain];
}
-(IBAction) save: (id) sender
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	NSButtonCell *selCell = [mGenderRadioButton selectedCell];

	[mInfo setNickname: [mNickname stringValue]];
	[mInfo setDate: [mDatePicker dateValue]];
	[mInfo setAge: [mAgeText intValue]];
	[mInfo setIsMale: (BOOL)[selCell tag]];
	if (![NSKeyedArchiver archiveRootObject: mInfo toFile: [self defaultFilePath]]) {
		NSLog(@"Archiving failed");
	}
	bSaved = YES;
	[pool drain];
}
-(IBAction) clear: (id) sender
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	[mInfo clear];
	[self updateUI];
	bSaved = NO;
	[pool drain];
}
-(IBAction) ageTextChanged: (id) sender
{
	NSLog(@"ageTextChanged");
	[mAgeSlider setIntValue: [mAgeText intValue]];
	bSaved = NO;
}
-(IBAction) ageSliderChanged: (id) sender
{
	NSLog(@"ageSliderChanged");
	[mAgeText setIntValue: [mAgeSlider intValue]];
	bSaved = NO;
}
-(IBAction) contentsChanged: (id) sender
{
	bSaved = NO;
}
-(void) updateUI
{
	[mNickname setStringValue: [mInfo nickname]];
	[mAgeText setIntValue: [mInfo age]];
	[mDatePicker setDateValue: [mInfo date]];
	[mGenderRadioButton selectCellWithTag: [mInfo isMale]];
}
-(void) initSlider
{
	[mAgeSlider setMinValue: 1];
	[mAgeSlider setMaxValue: 120];
	[mAgeSlider setIntValue: [mAgeText intValue]];
}
- (NSApplicationTerminateReply) applicationShouldTerminate:(NSApplication *)sender
{
	NSApplicationTerminateReply reply = NSTerminateNow;
	if (!bSaved) {
		NSLog(@"Not saved yet");
		int iResponse = 
		NSRunAlertPanel(@"Unsaved Info", 
						@"Are you sure you want to quit app without saving the info?",
						@"OK", @"Cancel", /*ThirdButtonHere:*/nil
						/*, args for a printf-style msg go here */);
		if (iResponse == NSAlertAlternateReturn)
			reply = NSTerminateCancel;
	}
	return reply;
}
-(NSString *) defaultFilePath
{
	return [NSHomeDirectory() stringByAppendingString: @"/.info.pkl"];
}
-(void) dealloc
{
	[mInfo release];
	[super dealloc];
}
@end